@tool
extends Control

signal add_tag(tag:String)
signal remove_tag(tag:String)

const TAG_LABEL = preload("res://addons/gameplay-ability-system/Scripts/Inspector/components/tag_comp/TagLabel.tscn")

var tags:Array[String]

func _ready() -> void:
	draw_tags(tags)
	set_tags_option()


func draw_tags(tags_to_list):
	for child in %TagContainer.get_children():
		child.queue_free()
	
	for tag in tags_to_list:
		var tag_label = TAG_LABEL.instantiate()
		tag_label.text = tag
		tag_label.report_tag.connect(_on_remove_tag)
		%TagContainer.add_child(tag_label)


func set_tags_option():
	%TagsOption.clear()
	for tag in TagSystem.global_tags:
		%TagsOption.add_item(tag)
	%TagsOption.select(-1)


func _on_add_pressed() -> void:
	if %TagsOption.selected == -1:
		printerr("请选择一个Tag再添加")
		return
		
	var new_tag = TagSystem.global_tags[%TagsOption.selected]
	if not tags.has(new_tag):
		add_tag.emit(new_tag)
		%TagsOption.select(-1)


func _on_remove_tag(tag:String) -> void:
	remove_tag.emit(tag)
